﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Convert.ToInt32(Request.QueryString["AddressId"]) > 0)
                {
                    btAdd.Visible = false;
                    ShowCountry();
                    ddlTown.Visible = false;
                    ddlCounty.Visible = false;
                    LoadAddressById();

                }
                else
                {
                    btUpdate.Visible = false;
                    //ShowTown();
                    ShowCountry();
                    //ShowCounty();
                }
                
            }
            
        }
        private void LoadAddressById()
        {
            int id = Convert.ToInt32(Request.QueryString["AddressId"]);            
            var biz = new AddressBiz();
            var addr = new Address();                       
            addr = biz.getbyId(id);
            txtPostCode.Text = addr.PostCode;
            ddlTown.SelectedValue = Convert.ToString(addr._Town.TownId);
            ddlCounty.SelectedValue = Convert.ToString(addr._County.CountyId);
            ddlCountry.SelectedValue = Convert.ToString(addr._Country.CountryId);
            txtAddress.Text = addr._Address;
            
        }
        private void ShowCountry()
        {
            var listCountry = new List<Country>();
            var biz = new CountryBiz();
            listCountry = biz.GetAllCountry();
            ddlCountry.Items.Clear();
            ddlCountry.DataSource = listCountry;
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem(" --Chose-- ", "0"));
        }
        private void ShowCounty(int id)
        {
            var listCounty = new List<County>();
            var biz = new CountyBiz();
            listCounty = biz.GetAllCounty(id);
            ddlCounty.Items.Clear();
            ddlCounty.DataSource = listCounty;
            ddlCounty.DataTextField = "CountyName";
            ddlCounty.DataValueField = "CountyId";
            ddlCounty.DataBind();
            ddlCounty.Items.Insert(0, new ListItem(" --Chose-- ", "0"));
        }
        private void ShowTown(int id)
        {
            var listTown = new List<Town>();
            var biz = new TownBiz();
            listTown = biz.getAllTown(id);
            ddlTown.Items.Clear();
            ddlTown.DataSource = listTown;
            ddlTown.DataTextField = "TownName";
            ddlTown.DataValueField = "TownId";
            ddlTown.DataBind();
            ddlTown.Items.Insert(0, new ListItem(" --Chose-- ", "0"));
        }
        private bool CheckText()
        {
            if (txtPostCode.Text.Equals(""))
                return false;
            else if (txtAddress.Text.Equals(""))
                return false;
            else if (ddlTown.SelectedValue == "0")
                return false;

            return true;
        }
        protected void btAdd_Click(object sender, EventArgs e)
        {
            var biz = new AddressBiz();
            var address = new Address
            {                
                PostCode = txtPostCode.Text.ToString(),
                _Town = new Town { TownId = Convert.ToInt32(ddlTown.SelectedValue) },
                _County = new County { CountyId = Convert.ToInt32(ddlCounty.SelectedValue)},
                _Country= new Country{ CountryId = Convert.ToInt32(ddlCountry.SelectedValue)},
                _Address = txtAddress.Text.ToString()
            };
            if (CheckText() == true)
            {
                bool result = biz.addAddress(address);
                Response.Redirect("../Views/SearchAddress.aspx");
            }                                    
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            var biz = new AddressBiz();
            int id = Convert.ToInt32(Request.QueryString["AddressId"]);            
            var address = new Address
            {
                PostCode = txtPostCode.Text.ToString(),
                _Town = new Town { TownId = Convert.ToInt32(ddlTown.SelectedValue) },
                _County = new County { CountyId = Convert.ToInt32(ddlCounty.SelectedValue) },
                _Country = new Country { CountryId = Convert.ToInt32(ddlCountry.SelectedValue) },
                _Address = txtAddress.Text.ToString(),
                AddressId = id 
            };
            bool result = biz.updateAddress(address);
            if (result == true)
            {
                Response.Redirect("../Views/SearchAddress.aspx");
            }
        }

        private void ClearText()
        {
            txtPostCode.Text = string.Empty;
            txtAddress.Text = string.Empty;
        }
        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ddlCountry.SelectedValue);
            ShowCounty(id);
        }

        protected void ddlCounty_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ddlCounty.SelectedValue);
            ShowTown(id);
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            ClearText();
        }
    }
}